app.controller('newExam', ['$scope','$http','$rootScope', '$state','$sessionStorage',function($scope,$http,$rootScope,$state,$sessionStorage){
	$scope.topicType=["单选题","多选题","判断题"];

	$http({
	    url:$rootScope.baseUrl+"Exercises/getSelectList",
	    method:"post",
	}).then(function(res){
	    if(res.data.code==200){
	    	$scope.selectList=res.data.data;

	    	$http({
	    	    url:$rootScope.baseUrl+"Exercises/getItemDetail",
	    	    method:"post",
	    	    data:{
	    	    	id:$sessionStorage.topicID
	    	    }
	    	}).then(function(res){
	    	    if(res.data.code==200){
	    	    	$scope.infoList=res.data.data;

	    	    	$scope.nowTopic=$scope.topicType[$scope.infoList.type-1];
	    	    	if($scope.nowTopic=="多选题"){
	    	    		$scope.inputType="checkbox";
	    	    		$scope.judgment=false;
	    	    		$scope.notJudgment=true;
	    	    	}else if($scope.nowTopic=="单选题"){
	    	    		$scope.inputType="radio";
	    	    		$scope.judgment=false;
	    	    		$scope.notJudgment=true;
	    	    	}else {
	    	    		$scope.judgment=true;
	    	    		$scope.notJudgment=false;
	    	    	}

	    	    	$scope.nowTheme=$scope.selectList.themeArr[$scope.infoList.theme_id];//试题分类
	    	    	$scope.nowChapter=$scope.selectList.chapterArr[$scope.infoList.chapter_id];//试题章节

	    	    	$scope.title=$scope.infoList.title;
	    	    	$scope.analysis=$scope.infoList.analysis;
	    	    	$scope.opt=$scope.infoList.opt;

	    	    	var right=[];
	    	    	$.each($scope.opt,function(index,item){
	    	    		if(item.is_true==1){
	    	    			right.push($scope.option[index]);
	    	    		}

	    	    		item.option=$scope.option[index];
	    	    	});

	    	    	$scope.right=right.join(",");
	    	    }else if(res.data.code==409){
			    	$state.go("access.signin");
			    }
	    	},function(error){
	    	    console.log(error);
	    	});
	    }
	},function(error){
	    console.log(error);
	});

	// 新增选项
	$scope.addOption=function(){
		var data={
			option:$scope.option[$scope.opt.length]
		}
		$scope.opt.push(data);
	}

	// 选项类型
	$scope.option=["A","B","C","D","E","F","G","H","I","J","K","L","M","","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];

	// 选择试题题型
	$scope.selectTopicType=function(item){
		$scope.nowTopic=item;

		if(item=="多选题"){
			$scope.inputType="checkbox";
			$scope.judgment=false;
			$scope.notJudgment=true;
			$scope.num=3;
		}else if(item=="单选题"){
			$scope.inputType="radio";
			$scope.judgment=false;
			$scope.notJudgment=true;
			$scope.num=3;
		}else {
			$scope.judgment=true;
			$scope.notJudgment=false;
			$scope.num=1;
		}

		++$scope.num;
		$scope.answer=$scope.option.slice(0,$scope.num);
	}
	

	// 选择试题分类
	$scope.selectTheme=function(item){
		$scope.nowTheme=item;
	}

	// 选择试题章节
	$scope.selectChapter=function(item){
		$scope.nowChapter=item;
	}

	// 选择正确答案
	$scope.selectAnswer=function(item,e){
		var right=[];
		var $input=$(".form-horizontal").find(".checkBox");

		$.each($input,function(index,ele){
			if($(ele).prop("checked")){
				right.push($(ele).closest(".form-group").find(".labelStyle").html().split("选项")[1]);
			}
		});

		$scope.right=right.join(",");
	}

	// 保存
	$scope.save=function(){
		var $optionDiv=$(".optionDiv")
		var $content=$optionDiv.find(".simditor-body");
		var $checkBox=$optionDiv.find(".checkBox");
		var $checked=$optionDiv.find(".checkBox:checked");

		if($scope.title==undefined){
			swal("请填写题干");
			return;
		}

		if($checked.length==0){
			swal("请选择正确答案");
			return;
		}else {
			var answer="";
			$.each($checked,function(index,ele){
				answer+=$(ele).closest(".radio").next().html().slice(2)+",";
			});
			answer=answer.slice(0,-1);
		}

		var optValArr=[];
		var count=0;
		if($scope.nowTopic=="单选题" || $scope.nowTopic=="多选题"){
			$.each($content,function(index,ele){
				var html=$(ele).html();
				optValArr.push(html);


				html=html.replace(/<[^>]+>/g,"");

				if(html==""){
					count++;
				}
			});

			var type=1;
			if($scope.nowTopic=="多选题"){
				var type=2;
			}
		}else{
			var type=3;

			$content=$optionDiv.find(".option");
			$.each($content,function(index,ele){
				var val=$(ele).val();
				optValArr.push(val);
				if(val==""){
					count++;
				}
			});
		}

		if(count!=0){
			swal("请填写选项内容");
			return;
		}
		

		if($scope.analysis==undefined){
			swal("请填写解析");
			return;
		}

		$http({
		    url:$rootScope.baseUrl+"Exercises/editExer",
		    method:"post",
		    data:{
		    	id:$sessionStorage.topicID,
		    	type:type,
		    	theme_id:$scope.nowTheme.id,
		    	chapter_id:$scope.nowChapter.id,
		    	title:$scope.title,
		    	analysis:$scope.analysis,
		    	opt_val_arr:JSON.stringify(optValArr),
		    	answer:answer
		    }
		}).then(function(res){
		    if(res.data.code==200){
		    	$state.go("fullScore.topicManage");
		    }else if(res.data.code==409){
		    	$state.go("access.signin");
		    }
		},function(error){
		    console.log(error);
		});
	}

	$scope.cancel=function(){
		swal({
		    title: "确定取消编辑？",
		    type: "warning",
		    showCancelButton: true,
		    confirmButtonColor: "#619ce1",
		    confirmButtonText: "确定",
		    closeOnConfirm: true
		},
		function() {
		    $state.go("fullScore.topicManage");
		});
	}
}]);